import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:practice_app/utils/storage.dart';
import 'dart:ui' as ui show window;
import 'const.dart';

/// 检查是否有 token
Future<bool> isAuthenticated() async {
  var profileJSON = StorageUtil().getJSON(STORAGE_USER_PROFILE_KEY);
  return profileJSON != null ? true : false;
}

/// 删除缓存 token
Future deleteAuthentication() async {
  await StorageUtil().remove(STORAGE_USER_PROFILE_KEY);
  // Global.profile = null;
}

/// 设置宽度
double doSetWidth(double width) {
  return ScreenUtil().setWidth(width);
}

/// 设置高度
double doSetHeight(double height) {
  return ScreenUtil().setWidth(height);
}

/// 设置字体
double doSetFontSize(double fontSize) {
  return ScreenUtil().setSp(fontSize);
}

/// 电话号码
bool doIsPhone(String input) {
  RegExp value = RegExp(
      r'^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$');
  return value.hasMatch(input);
}

/// 获取appbar的高度
double getAppBarHeight() {
  return MediaQueryData.fromWindow(ui.window).padding.top;
}

///枚举类型转string
String enumToString(o) => o.toString().split('.').last;

///string转枚举类型
T enumFromString<T>(Iterable<T> values, String value) {
  return values.firstWhere((type) => type.toString().split('.').last == value,
      orElse: () => null!);
}

///
/// 设置小于10 前面加数字
///
String numberAddZero(int num) {
  if (num < 10) {
    return '0$num';
  }
  return '$num';
}
